package com.uday.kraterchat.chatclient.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import com.uday.kraterchat.chatclient.business.UserServiceDelegate;
	import com.uday.kraterchat.chatclient.control.LogoutUserEvent;
	import com.uday.kraterchat.chatclient.control.SubscribeMsgEvent;
	import com.uday.kraterchat.chatclient.data.UserInfoBaseItem;
	import com.uday.kraterchat.chatclient.model.ChatClientModel;
	
	import flash.desktop.NativeApplication;
	
	import mx.rpc.IResponder;

	public class LogoutUserCommand implements ICommand, IResponder
	{
		private var objChatClientModel:ChatClientModel = ChatClientModel.getInstance();
			
		public function LogoutUserCommand()
		{
		}

		public function execute(event:CairngormEvent):void
		{
			var objUserInfoBaseItem:UserInfoBaseItem = (event as LogoutUserEvent).objDataItem as UserInfoBaseItem;
			if(objUserInfoBaseItem && (objUserInfoBaseItem.lngUserID)){
				var objLoginUserDel:UserServiceDelegate = new UserServiceDelegate(this);
				objLoginUserDel.logoutUser((event as LogoutUserEvent).objDataItem as UserInfoBaseItem);
				objChatClientModel.objCurrUser = null;
			} else if(objChatClientModel.blnIsApplClosed == true){
				result(null);
			}
		}
		
		public function result(data:Object):void
		{
			if(objChatClientModel.blnIsApplClosed){
				objChatClientModel.objChatWindow.close();
				NativeApplication.nativeApplication.activeWindow.close();
			} else{
				objChatClientModel.mainAppState = KraterChatAir.STATE_LOGIN;
			}
		}
		
		public function fault(info:Object):void
		{
			if(objChatClientModel.blnIsApplClosed){
				objChatClientModel.objChatWindow.close();
				NativeApplication.nativeApplication.activeWindow.close();
		
			} else{
				objChatClientModel.mainAppState = KraterChatAir.STATE_LOGIN;
			}
		}
		
	}
}